<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <!--
            getTotalPrice方法会调用三次，而totalPrice计算属性只调用了一次

            结论：computed内部方法的缓存起了作用
        -->
        <p>总价格(methods): {{getTotalPrice()}}</p>
        <p>总价格(methods): {{getTotalPrice()}}</p>
        <p>总价格(methods): {{getTotalPrice()}}</p>
        <hr>
        <p>总价格(computed): {{totalPrice}}</p>
        <p>总价格(computed): {{totalPrice}}</p>
        <p>总价格(computed): {{totalPrice}}</p>
        <hr>
        <button @click="changePrice">改变价格</button>
    </div>
</body>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            arr: [{
                name: 'Vuejs入门',
                price: 99,
                count: 3
            }, {
                name: 'Vuejs底层',
                price: 89,
                count: 1
            }, {
                name: 'Vuejs从入门到放弃',
                price: 19,
                count: 5
            }, ]
        },
        methods: {
            getTotalPrice() {
                console.log('方法调用')
                return this.arr.reduce((prev, current) => {
                    let count = prev + (current.price * current.count)
                    return count
                }, 0)
            },
            changePrice() {
                this.arr[0].price++
            }
        },
        //计算属性
        computed: {
            totalPrice() {
                console.log('计算属性')
                return this.arr.reduce((prev, current) => {
                    let count = prev + (current.price * current.count)
                    return count
                }, 0)
            }
        }
    })
</script>

</html>